package maze;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import maze.ui.MazeViewer;

public class launchMaze {

	private static String loadconfig() {
		
		String returnString = null;
		
		File mazeTypeFile = new File(ConfigLoading.stylePath);
		BufferedReader mazeTypeBuff = null;
		try {
			mazeTypeBuff = new BufferedReader(new FileReader(mazeTypeFile));
			
			String mazeType = null;
			Scanner sc = null;
			String StyleProper = null;
			
			while((mazeType = mazeTypeBuff.readLine())!=null){
				
				sc = new Scanner(mazeType);
				sc.useDelimiter("=");
				
				if(sc.hasNext()) {
					StyleProper = sc.next();
					if(StyleProper.equals("style")) {
						
						if(sc.hasNext()) {
							returnString = sc.next();
						}
					}
					else if(StyleProper.equals("mazePath")) {
						if(sc.hasNext()) {
							ConfigLoading.loadMazePath = sc.next();
						}
					}
				}
				
				sc.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				mazeTypeBuff.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return returnString;
	}
	
	public static void main(String[] args) {
		
		ConfigLoading.stylePath = "style.maze";
		
		String style = loadconfig();
		
		MazeBuilder createmaze = new FactoryMazeBuilder();
		
		if(style.equals("maze.FinalFormMazeGame")) {
			createmaze = new FactoryMazeBuilder();
		}else if(style.equals("maze.PrototypeMazeGame")) {
			createmaze = new SimpleMazeBuilder();
		}
		
		Maze maze = createmaze.getMaze();
		
	    MazeViewer viewer = new MazeViewer(maze);
	    viewer.run();
	}

}
